﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.BasicNet.Core.Schedule
 * 接口名称 ScheduleInfo
 * 开发人员：11920
 * 创建时间：2023/4/10 15:17:52
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Framework.Schedule
{ 
	public class ScheduleInfo
	{
		public ScheduleInfo(bool isAsync)
		{
			Period = 1000 * 10;//系统默认是十秒等待时间
			Id = Guid.NewGuid();
			IsAsync = isAsync;
			BeforeStartWait = 1000;
		}
		public eScheduleStatus ScheduleStatus { get { return scheduleStatus; } set { scheduleStatus = value; } }
		public string ErrorMessage { get { return errorMessage; } set { errorMessage = value; } }
		public string Title { get { return title; } set { title = value; } }
		public string Description { get { return description; } set { description = value; } }
		public long Period { get { return period; } set { period = value; } }
		public long BeforeStartWait { get { return beforeStartWait; } set { beforeStartWait = value; } }
		public Guid Id { get { return id; } private set { id = value; } }
		public bool IsAsync { get { return isAsync; }  set { isAsync = value; } }

		/// <summary>
		/// 整点执行 ，未到整点将等待
		/// 整点执行
		/// 1-分钟 2-小时 3-天
		/// </summary>
		/// <param name="len">时长</param>
		/// <param name="type">1-秒 2-分钟  3-小时 4 -天</param>
		public void SetWholeTime(int type,int len)
		{
			var now = DateTime.Now;

			if (type==1)
			{
				int remainderSecond = now.Second % len;
				if (remainderSecond==0)
				{
					this.BeforeStartWait = 0;
				}
				else
				{
					this.BeforeStartWait = TimeSpan.TicksPerSecond * (len - remainderSecond);
				}
				this.Period = TimeSpan.TicksPerSecond * len;
			}
			else if (type == 3)
			{
				var remainderHour = now.Hour % len;
				if (remainderHour == 0)
				{
					if (now.Minute == 0 && now.Second == 0)
					{
						this.BeforeStartWait = 0;
					}
					else
					{
						var temp = new DateTime(now.Year, now.Month, now.Day, now.Hour + len, 0, 0);
						this.BeforeStartWait = (temp - now).Ticks;
					}
				}
				else
				{
					var temp = new DateTime(now.Year, now.Month, now.Day, now.Hour + len - remainderHour, 0, 0);
					this.BeforeStartWait = (temp - now).Ticks;
				}
				this.Period = TimeSpan.TicksPerHour * len;//10s
			}
			else if (type ==2 )
			{
				var remainderMinute = now.Minute % len;
				if (remainderMinute != 0)
				{
					this.BeforeStartWait = TimeSpan.TicksPerMinute * (len - remainderMinute);
				}
				this.Period = TimeSpan.TicksPerMinute * len;//10s
			}
			else if (type == 4)
			{
				var dayNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
				if (DateTime.Now != dayNow)
				{
					this.BeforeStartWait = (dayNow.AddDays(len) - DateTime.Now).Ticks;
				}
				this.Period = TimeSpan.TicksPerDay * len;
			}
		}

		private eScheduleStatus scheduleStatus;
		private string errorMessage;
		private string title;
		private string description;
		private long period;
		private long beforeStartWait;
		private Guid id;
		private bool isAsync;
	}
}
